package com.derbysoft.dswitch.adapter.ihg.common.utils;

import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;

public abstract class CloneUtils {

    private static Log logger = LogFactory.getLog(CloneUtils.class);

    public static <T extends Serializable> T clone(T target) {
        if (target == null) {
            return null;
        }
        return (T) SerializationUtils.clone(target);
    }

    public static Object deepClone(Object object) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream);
            outputStream.writeObject(object);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            return new ObjectInputStream(byteArrayInputStream).readObject();
        } catch (Exception e) {
            logger.error("clone object error:" + e.getMessage());
        }
        return object;
    }
}

